package com.fyzk.activity.lottery.recommend_order;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fyzk.activity.fragment.RankListFragment;
import com.fyzk.activity.indicator.ScaleTransitionPagerTitleView;
import com.fyzk.adapter.DocumentaryPagerAdapter;
import com.fyzk.base.BaseActivity;
import com.fyzk.bean.DocumentaryMenuResponse;
import com.fyzk.utils.AssetsUtil;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 排行榜
 */
@ContentView(R.layout.activity_rank_list)
public class RankListActivity extends BaseActivity {
    @ViewInject(R.id.recommenddetail_topview)
    private TopTitleView topView;
    @ViewInject(R.id.mMagicIndicator)
    private MagicIndicator mMagicIndicator;
    @ViewInject(R.id.mViewPager)
    private ViewPager mViewPager;

    private List<DocumentaryMenuResponse> indicatorList;
    private List<Fragment> fragments = new ArrayList<>();
    private DocumentaryPagerAdapter adapter;

    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initData();
        initListener();
    }

    private void initData() {
        index = getIntent().getIntExtra("index", 0);

        initIndicator();
    }

    private void initIndicator() {
        if (null == indicatorList)
            indicatorList = new ArrayList<>();
        indicatorList.clear();

        try {
            String mMenuData = AssetsUtil.read(getContext(), "json/documentaryMenuNew.json");
            indicatorList = JSON.parseObject(mMenuData, new TypeReference<List<DocumentaryMenuResponse>>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return indicatorList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(context.getResources().getColor(R.color.race_color_1c));
                simplePagerTitleView.setSelectedColor(context.getResources().getColor(R.color.race_color_tab_text_color));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
                simplePagerTitleView.setText(indicatorList.get(index).getName());
                simplePagerTitleView.setOnClickListener(v -> mViewPager.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                linePagerIndicator.setStartInterpolator(new AccelerateInterpolator());
                linePagerIndicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                linePagerIndicator.setLineWidth(DensityUtil.dip2px(36));
                linePagerIndicator.setLineHeight(DensityUtil.dip2px(2));
                linePagerIndicator.setRoundRadius(DensityUtil.dip2px(2));
                linePagerIndicator.setColors(context.getResources().getColor(R.color.race_color_tab_text_color));
                return linePagerIndicator;
            }
        });
        mMagicIndicator.setNavigator(commonNavigator);

        for (int i = 0; i < indicatorList.size(); i++) {
            fragments.add(RankListFragment.newInstance(indicatorList.get(i).getType()));
        }
        adapter = new DocumentaryPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(indicatorList.size());

        ViewPagerHelper.bind(mMagicIndicator, mViewPager);

        mViewPager.setCurrentItem(index);
    }

    private void initListener() {
        topView.setOnTitleClickListener(this);
    }
}
